<?php

namespace App\Http\Controllers\Article\Web;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Article\ArticleCategoryValidate;
use App\Http\Validate\Article\ArticleValidate;
use App\Jobs\Article\ArticleOperate;
use App\Jobs\Export\Export;
use App\Models\Article\ArticleCategoryModel;
use App\Service\Article\ArticleCategoryService;
use App\Service\Article\ArticleService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ArticleCategoryController extends BaseController
{
    /**
     * 分类列表
     *
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function list(): JsonResponse
    {
        return $this->success(ArticleCategoryService::list(ArticleCategoryValidate::listValidate()));
    }

    /**
     * 添加分类
     *
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function add(): JsonResponse
    {
        return $this->success(ArticleCategoryService::add(ArticleCategoryValidate::addValidate()));
    }

    /**
     * 更新分类
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function update(Request $request, int $id): JsonResponse
    {
        ArticleCategoryService::update(ArticleCategoryValidate::updateValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }

    /**
     * 专题排序
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function sort(Request $request, int $id): JsonResponse
    {
        ArticleCategoryService::sort(ArticleCategoryValidate::sortValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }

    /**
     * 分类详情
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        $detail = ArticleCategoryService::detail(
            ArticleCategoryValidate::detailValidate($request->merge(["id" => $id])->all())
        );
        return $this->success($detail);
    }

    /**
     * 专题删除
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function del(Request $request, int $id): JsonResponse
    {
        ArticleCategoryService::del(
            ArticleCategoryValidate::delValidate($request->merge(["id" => $id])->all())
        );
        return $this->success();
    }


    /**
     * 专题文章列表
     *
     * @param Request $request
     * @param int $cid
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function articleList(Request $request, int $cid): JsonResponse
    {
        return $this->success(
            ArticleCategoryService::articleList(
                ArticleCategoryValidate::articleListValidate($request->merge(["id" => $cid])->all())
            )
        );
    }

    /**
     * 专题文章排序
     *
     * @param Request $request
     * @param int $cid
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function articleSort(Request $request, int $cid): JsonResponse
    {
        ArticleCategoryService::articleSort(
            ArticleCategoryValidate::articleSortValidate($request->merge(["id" => $cid])->all())
        );
        return $this->success();
    }

    /**
     * @param Request $request
     * @param int $cid
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function articleAdd(Request $request, int $cid): JsonResponse
    {
        ArticleCategoryService::articleAdd(
            ArticleCategoryValidate::articleAddValidate($request->merge(["id" => $cid])->all())
        );
        return $this->success();
    }

    /**
     * @param Request $request
     * @param int $cid
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function articleDel(Request $request, int $cid): JsonResponse
    {
        ArticleCategoryService::articleDel(
            ArticleCategoryValidate::articleDelValidate($request->merge(["id" => $cid])->all())
        );
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function groupStatistics(Request $request): JsonResponse
    {
        $params = ArticleCategoryValidate::groupStatisticsValidate($request->all());
        if(attributeGet($params, "export", 0)){
            $id = attributeGet($params, "categoryId", 0);
            if ($id > 0) {
                $exportType     = Export::TYPE_CATEGORY_SINGLE_ANALYSIS;
                $articleCategoryModel   = ArticleCategoryModel::query()->where("id", $id)->first();
                $fileNamePrefix = "专题分析-" . attributeGet($articleCategoryModel, "name", "单篇");
            } else {
                $exportType     = Export::TYPE_CATEGORY_HOLISTIC_ANALYSIS;
                $fileNamePrefix = "专题分析-整体";
            }

            dispatch(
                (new Export(
                    "{$fileNamePrefix}-{$params->startTime}-{$params->endTime}",
                    $exportType,
                    [
                        'data'   => (array)$params,
                        'userId' => getRmsUserId(),
                        'id'     => $id
                    ]
                ))
            )->onQueue(Export::QUEUE_NAME);
            return $this->success([]);
        }

        return $this->success(ArticleCategoryService::groupStatistics($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function hotList(Request $request): JsonResponse
    {
        $params = ArticleCategoryValidate::hotListValidate($request->all());
        return $this->success(ArticleCategoryService::hotList($params));
    }

    /**
     * 置顶接口
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function putTop(Request $request, int $id): JsonResponse
    {
        ArticleCategoryService::putTop(
            ArticleCategoryValidate::putTopValidate($request->merge(["id" => $id])->all()),
            $request->user()->id
        );
        return $this->success();
    }
}
